﻿using FrameManager;
using GeneralLib;
using IOModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OPX_FlowGlueMachine
{
    public static class MachineStatusScaner
    {
        static bool bPreStartButton = false;
        static bool bCurStartButton = true;
        static bool bPreResetButton = false;
        static bool bCurResetButton = true;
        static bool bPreStopButton = false;
        static bool bCurStopButton = true;
        //static bool bPreAutoManualButton = false;
        //static bool bCurAutoManualButton = true;
        //static bool bCurDoorSenser = false;
        //static int iPreDoorStatus = 0;
        //static int iCurDoorStatus = 0;
        //static bool bPreLightSenser = true;
        //static bool bCurLightSenser = false;
        static bool bPreEstopButton = true;
        static bool bCurEstopButton = false;
        //static bool bFreshOn = false;
        static bool bAlreadyBizz = false;
        public static void FreshMachineStatus()
        {
            
            
                #region 开始按键处理
                bCurStartButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Start].ON;
                if (bCurStartButton != bPreStartButton)
                {
                    if (bCurStartButton)
                    {
                        FrameManager.MachineStatus.SetMachineStartAction();
                    }
                }
                bPreStartButton = bCurStartButton;
                #endregion

                #region 复位按键处理
                bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                if (bCurResetButton != bPreResetButton)
                {
                    if (bCurResetButton)
                    {
                        FrameManager.MachineStatus.ResetMachineAlarmAction();
                    }
                }
                bPreResetButton = bCurResetButton;
                #endregion

                #region 停止按键处理
                bCurStopButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Stop].ON;
                if (bCurStopButton != bPreStopButton)
                {
                    if (bCurStopButton)
                    {
                        FrameManager.MachineStatus.SetMachineStopAction();
                    }
                }
                bPreStopButton = bCurStopButton;
                #endregion

               

                #region 急停按键处理
                bCurEstopButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.EStop].ON;
                if (bCurEstopButton != bPreEstopButton)
                {
                    EstopChange(!bCurEstopButton);
                }
                bPreEstopButton = bCurEstopButton;
                #endregion

                #region RedAndGreen
                if (MachineStatus.bStart)
                {

                    if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.ButtonGreen].ON == false)
                    {
                        IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.ButtonGreen].ON = true;
                    }
                    if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.TowerGreen].ON == false)
                    {
                        IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.TowerGreen].ON = true;
                    }
                    if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.ButtonRed].ON == true)
                    {
                        IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.ButtonRed].OFF = true;
                    }
                    if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.TowerRed].ON == true)
                    {
                        IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.TowerRed].OFF = true;
                    }
                }
                else
                {

                    if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.ButtonGreen].ON == true)
                    {
                        IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.ButtonGreen].OFF = true;
                    }
                    if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.TowerGreen].ON == true)
                    {
                        IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.TowerGreen].OFF = true;
                    }
                    if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.ButtonRed].ON == false)
                    {
                        IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.ButtonRed].ON = true;
                    }
                    if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.TowerRed].ON == false)
                    {
                        IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.TowerRed].ON = true;
                    }
                }
                #endregion
                #region Yellow
                if (MachineStatus.bAlarm || MachineStatus.bRemaid)
                {

                    if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.ButtonYellow].ON == false)
                    {
                        IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.ButtonYellow].ON = true;
                    }
                    if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.TowerYellow].ON == false)
                    {
                        IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.TowerYellow].ON = true;
                    }
                    if (MachineStatus.bBizzDisable)
                    {
                        if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.Bizz].ON == true)
                        {
                            IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.Bizz].OFF = true;
                        }
                    }
                    else
                    {
                        if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.Bizz].ON == false)
                        {
                            if (bAlreadyBizz == false)
                            {
                                IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.Bizz].ON = true;
                                bAlreadyBizz = true;
                            }
                        }
                    }
                }
                else
                {
                    bAlreadyBizz = false;
                    if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.ButtonYellow].ON == true)
                    {
                        IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.ButtonYellow].OFF = true;
                    }
                    if (IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.TowerYellow].ON == true)
                    {
                        IOManager.ioSetting.listStandOutputs[(int)StandOutPutName.TowerYellow].OFF = true;
                    }
   
                }
                #endregion
            
        }

        private static void EstopChange(bool bStatus)
        {
            if (bStatus)
            {
                FrameManager.MachineStatus.setMachineEstopAction();
            }
            else
            {
                FrameManager.MachineStatus.resetMachineEstopAction();
            }
        }
    }
}
